package com.yt.bigdata;

import com.y.report.hepler.ResultSetUtil;

import java.sql.*;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class HiveJdbcClient {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://10.192.112.15:10000/default";
    private static String username = "zt17606";
    private static String password = "";
    private static String sql = "set hive.execution.engine=spark";

//    private static String driverName = "com.mysql.cj.jdbc.Driver";
//    private static String url = "jdbc:mysql://10.192.147.1:9030";
//    private static String username = "root";
//    private static String password = "root";
//    private static String sql = "ADMIN SHOW REPLICA DISTRIBUTION FROM  doris_plugin_db__.doris_audit_tbl__;";

    public static void main(String[] args) throws SQLException {
        String userCredentials = "admin" + ":" + "admin";
        String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
        System.out.println(111);


        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
//            ResultSet resultSet1 = statement.executeQuery(sql);
            boolean execute = statement.execute(sql);
//            boolean execute2 = statement.execute("set hive.execution.engine");
            ResultSet rt = statement.getResultSet();
            if (rt != null) {
                List<Map<String, Object>> list = ResultSetUtil.getList(rt);
                System.out.println(list);
            }

            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
